package com.gfwy.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import com.gfwy.dao.IConfigDao;
import com.gfwy.vo.Config;

public class ConfigDaoImpl implements IConfigDao {
	private Connection conn = null;
	private PreparedStatement pstmt = null;
	private ResultSet rs = null;
	public ConfigDaoImpl(Connection conn) {
		this.conn = conn;
	}
	@Override
	public boolean doUpdate(Config config) throws Exception {
		boolean flag = false;
		try {
			String sql = "UPDATE config SET copyright=?,tech=?,email=?,address=?,postcode=?,tel=? WHERE cid=?";
			this.pstmt = this.conn.prepareStatement(sql);
			this.pstmt.setString(1, config.getCopyright());
			this.pstmt.setString(2, config.getTech());
			this.pstmt.setString(3, config.getEmail());
			this.pstmt.setString(4, config.getAddress());
			this.pstmt.setString(5, config.getPostcode());
			this.pstmt.setString(6, config.getTel());
			this.pstmt.setInt(7, config.getCid());
			if(this.pstmt.executeUpdate() > 0) {
				flag = true;
			}
		} catch (Exception e) {
			throw e;
		} finally {
			if(this.pstmt != null) {
				try {
					this.pstmt.close();
				} catch (Exception e) {
					throw e;
				}
			}
		}
		return flag;
	}

	@Override
	public Config findById(int cid) throws Exception {
		Config config = null;
		try {
			String sql = "SELECT * FROM config WHERE cid=?";
			this.pstmt = this.conn.prepareStatement(sql);
			this.pstmt.setInt(1, cid);
			this.rs = this.pstmt.executeQuery();
			if(this.rs.next()) {
				config = new Config();
				config.setCid(this.rs.getInt("cid"));
				config.setCopyright(this.rs.getString("copyright"));
				config.setTech(this.rs.getString("tech"));
				config.setAddress(this.rs.getString("address"));
				config.setEmail(this.rs.getString("email"));
				config.setPostcode(this.rs.getString("postcode"));
				config.setTel(this.rs.getString("tel"));
			}
		} catch (Exception e) {
			throw e;
		} finally {
			if(this.rs != null) {
				try {
					this.rs.close();
				} catch (Exception e) {
					throw e;
				}
			}
			if(this.pstmt != null) {
				try {
					this.pstmt.close();
				} catch (Exception e) {
					throw e;
				}
			}
		}
		return config;
	}

}
